package com.ruijie.acl.type;

import java.util.ArrayList;
import java.util.List;

/**
 * 源目端口的枚举类
 * @author aiti-guest
 *
 */
public enum PortTypeEnum { 
	bgp("bgp",179, "tcp"),
	chargen("chargen",19, "tcp"),
	cmd("cmd",514, "tcp"),
	daytime("daytime",13, "tcp"),
	biff("biff", 512 , "udp"),
	bootpc("bootpc", 68, "udp"),
	bootps("bootps", 67, "udp"),
	discard("discard",9, "all"),
	dnsix("dnsix", 195, "udp"),
	domain("domain",53, "all"),
	echo("echo",7, "all"),
	exec("exec",512, "tcp"),
	finger("finger",79, "tcp"),
	ftp("ftp",21, "tcp"),
	ftp_data("ftp-data",20, "tcp"),
	gopher("gopher",70, "tcp"),
	hostname("hostname",101, "tcp"),
	ident("ident",113, "tcp"),
	irc("irc",194, "tcp"),
	klogin("klogin",543, "tcp"),
	kshell("kshell",544, "tcp"),
	login("login",513, "tcp"),
	lpd("lpd",515, "tcp"),
	nntp("nntp",119, "tcp"),
	isakmp("isakmp", 500, "udp"),
	mobile_ip("mobile-ip", 434, "udp"),
	nameserver("nameserver", 42, "udp"),
	netbios_dgm("netbios-dgm", 138, "udp"),
	netbios_ns("netbios-ns", 137, "udp"),
	netbios_ss("netbios-ss", 139, "udp"),
	ntp("ntp", 123, "udp"),
	pim_auto_rp("pim-auto-rp",496, "all"),
	pop2("pop2",109, "tcp"),
	pop3("pop3",110, "tcp"),
	smtp("smtp",25, "tcp"),
	rip("rip", 520, "udp"),
	snmp("snmp", 161, "udp"),
	snmptrap("snmptrap", 162, "udp"),
	sunrpc("sunrpc",111, "all"),
	syslog_conn("syslog-conn",601, "tcp"),
	syslog("syslog", 514, "udp"),
	tacacs("tacacs",49, "all"),
	talk("talk",517, "all"),
	telnet("telnet",23, "tcp"),
	tftp("tftp", 69, "udp"),
	time("time",37, "all"),
	uucp("uucp",540, "tcp"),
	whois("whois",43, "tcp"),
	www("www",80, "tcp"),
	who("who", 513, "udp"),
	xdmcp("xdmcp", 177, "udp");
	
	private String value;
	private int port;
	private String type;

	PortTypeEnum(String value,int port, String type){
		this.value=value;
		this.port=port;
		this.type = type;
	}
	

	public int port(){
		return this.port;
	}
	
	public String value() {
		return this.value;
	}
	
	public String type(){
		return this.type;
	}

   
	
	/**
     * 根据端口的数值和协议类型获取枚举
     * @param port
     * @return
     */
	public static PortTypeEnum getEnum(int port,String protocolType){
		if(protocolType==null){
			return null;
		}
		for(PortTypeEnum e:PortTypeEnum.values()){
			if(e.port==port&&("all".equals(e.type)||e.type.equals(protocolType))){
				return e;
			}
		}
		return null;
	}
	
	/**
	 * 根据字符串value获取枚举
	 * @param value
	 * @return
	 */
	public static PortTypeEnum getEnum(String value){
		if(value==null||value.trim().length()==0){
			return null;
		}
		for(PortTypeEnum e:PortTypeEnum.values()){
			if(e.value.equals(value)){
				return e;
			}
		}
		return null;
	}
	
	/**
	 * 根据字符串value获取端口对应的数值
	 * @param value
	 * @return
	 */
	public static String getEnumValue(String value){
		PortTypeEnum protTypeEnum= getEnum(value);
		if(protTypeEnum!=null){
			return String.valueOf(protTypeEnum.port);
		}else{
			return null; 
		}
	}
	/**
	 * 根据字符串value和协议类型获取端口对应的数值
	 * @param value
	 * @param type 协议类型tcp||udp
	 * @return
	 */
	public static String getEnumValue(String value, String type){
		if (type != null){
			PortTypeEnum portTypeEnum= getEnum(value);
			if(portTypeEnum!=null){
				if ("all".equals(portTypeEnum.type()) || portTypeEnum.type().equals(type.toLowerCase())){
					return String.valueOf(portTypeEnum.port);
				}
			}
		}
		return null;
	}
	
	/**
	 * 根据端口的数值和协议类型，获取端口对应的字符串
	 * @param portInt
	 * @param protocolType
	 * @return 查找不到，返回portInt值
	 */
	public static String getPortString(String portInt,String protocolType){
		if(portInt==null||protocolType==null){
			return portInt;
		}
		int port=0;
		try{
			port=Integer.parseInt(portInt);
		}catch(Exception e){
			return portInt;
		}
		PortTypeEnum portTypeEnum=PortTypeEnum.getEnum(port,protocolType);
		if(portTypeEnum!=null){
			String type=protocolType.toLowerCase();
			if("tcp".equals(type)&&
					(portTypeEnum.type().equals("all")||"tcp".equals(portTypeEnum.type()))
			 ||"udp".equals(type)&&
					(portTypeEnum.type().equals("all")||"udp".equals(portTypeEnum.type()))){
				return portTypeEnum.value;
			}
		}
		return portInt;
	}
	
	public static List<PortTypeEnum> getEnumList(String type){
		List<PortTypeEnum> list = new ArrayList<PortTypeEnum>();
		if (type != null && ("tcp".equals(type) || "udp".equals(type) || "all".equals(type))){
			for (PortTypeEnum entity : PortTypeEnum.values()){
				if ("all".equals(type)){
					list.add(entity);
				}else if ("tcp".equals(type) && ("tcp".equals(entity.type()) || "all".equals(entity.type()))){
					list.add(entity);
				}else if ("udp".equals(type) && ("udp".equals(entity.type()) || "all".equals(entity.type()))){
					list.add(entity);
				}
			}
		}
		return list;
	}
	
	/**
	 * 将端口字符串转为数值或将数值转为字符串
	 * @param value 
	 * @return
	 */
	public static String toggle(String value,String protocolType){
		if(value==null||value.length()==0){
			//端口没有输入，输出默认值：
			return value;
		}
		PortTypeEnum portTypeEnum=getEnum(value);
		if(portTypeEnum!=null){
			//将字符转换为数值
			return new Integer(portTypeEnum.port).toString();
		}else{
			try{
				int port=Integer.parseInt(value);
				portTypeEnum=getEnum(port,protocolType);
				if(portTypeEnum!=null){
					//将数值转换为字符串
					return portTypeEnum.value;
				}
			}catch(Exception e){
				//未知的端口类型
				return value;
			}
		}
		//未知的端口类型
		return value;
	}
	public static void main(String[] args){
		java.util.Scanner in=new java.util.Scanner(System.in);
		String port=null;
		String type=null;
		String input=null;
		while(!"exit".equals(input)){
			input=in.nextLine();
			String[] strs=input.split(",");
			if(strs.length==2){
				for(String s:strs){
					if("null".equals(s))s=null;
				}
				port=strs[0];
				type=strs[1];
			}else{
				continue;
			}
			System.out.println(getPortString(port,type));
		}
	}
}












